<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Point extends Auth_Controller {

    public function __construct()
    {
        parent::__construct();
        $this->load->model("Point_model","point");
		$this->load->model("User_model","user");
    }
 
	public function pointCarouselList(){
        $this->isArrays = true;
        $pointList = array();
        $pointList = $this->point->getPointList();
        $this->createApi($pointList);
        exit();
    }

    public function get_point_by_id($id)
    {
        $this->db->where("id",$id);
        $query=$this->db->get($this->table);
        return $query->row_array();
    }

   //分享阅读添加积分
    public function setPoints(){
        $params['newsID'] = $this->queryData['newsID'];;
        $params['type'] = $this->queryData['type'];;
        $validate = new Validate([
            'newsID' => 'require|number',
            'type' => 'require|number',
        ]);
        if (!$validate->check($params)) 
        {
            $this->errorCode = 50003;
            $this->errorMessage = $validate->getError();
            $this->createApi();
            exit;
        }
        
        $userInfo = $this->userDetail;
        //判断今日获得积分上限数目
        $pointTotal = $this->point->todayPointTotal($userInfo['AccountID']);
		$max_points = $this->setPoint(7);
        if($pointTotal > $max_points)
        {
            $this->errorCode = 90001;
            $this->createApi();exit;
        }

        
         if(in_array($params['type'],[3,4]))
        {
			$this->load->model("News_model","news");
            $newsDetail = $this->news->getNewsDetail($params['newsID']);
            if(!$newsDetail)
            {
                $this->errorCode = 50008;
                $this->createApi();exit;
            }
            if($newsDetail['point'])
            {
                $points = $newsDetail['point'];
            }
        }else
        {
            $points = $this->setPoint($params['type']);
        }
        $data = array(
            'AccountID'=>$userInfo['AccountID'],
            'createtime'=>date('Y-m-d',time()),
            'points'=>$points,
            'type'=>$params['type'],
            'typeid'=>$params['newsID'],
            );
        $res = $this->point->setPoints($data);
        if($res === 'exists')
        {
            $this->errorCode = 50007;
            $this->createApi();exit;
        }
        if(!$res)
        {
            $this->errorCode = 50006;
            $this->createApi();exit;
        }
        $this->createApi(['addPointCnt'=>$points]);exit;
    }
	
	public function totalPoint(){
		$userInfo = $this->userDetail;
		$this->point->totalPoint(['AccountID'=>$userInfo['AccountID']]);
	}
	
	public function continuingTimes()
	{
		$this->isArrays = true;
		$points = $this->setPoint(2);
		$point_max = $this->setPoint(7);
		$lianxu = $this->setPoint(8);
		$dizeng = $this->setPoint(6);
		$mypoint_max = ($lianxu-1)*$dizeng+$points;
		$times['continuing_times'] = -1;
		$userInfo = $this->userDetail;
		if($userInfo)
		{
			$times = $this->point->getPointByTime(date("Y-m-d",time()),$userInfo['AccountID']);
		}
		$result = [];
		for($i=0;$i<28;$i++)
		{
			if($i != 0)
			{
				$points = $points+$dizeng;
			}		
			if($i >= $lianxu || $points>=$point_max)
			{
				$points= $mypoint_max;
				//$points =  $result[$lianxu-1]['points'];
				
			}
			$result[$i]['points'] = $points;
			if($i<$times['continuing_times'])
			{
				$result[$i]['status'] = true;
				
			}else{
				$result[$i]['status'] = false;
			}
		}
		
		$this->createApi($result);exit;
	}
	
	
	public function sendPoint(){
        $params['points'] = $this->queryData['points'];
        $params['mobile'] =$this->queryData['mobile'];;
        $validate = new Validate([
            'points' => 'require|number',
            'mobile' => 'require|string',
        ]);
        if (!$validate->check($params)) {
            $this->errorCode = 50003;
            $this->errorMessage = $validate->getError();
            $this->createApi();
            exit;
        }
        $userInfo = $this->userDetail;
        if($params['points'] > $userInfo['points']){
            $this->errorCode = 90004;
            $this->createApi();exit();
        }
		$toUserInfo= $this->user->get_userinfo("", $params['mobile']);
		$result = 0;
		if(!isset($toUserInfo['AccountID']))
		{
			$this->errorCode = 90010;
			$this->createApi();exit;
		}
		$result = $this->point->toSendPoints($userInfo['AccountID'], $toUserInfo['AccountID'], $params['points']);
		if(empty($result))
		{
			$this->errorCode = 50006;
			$this->createApi();exit;
		}
		$data = array(
			'AccountID'=>$userInfo['AccountID'],
			'createtime'=>date('Y-m-d',time()),
			'points'=>($params['points']*(-1)),
			'type'=>7,
			'toAccountID'=> $toUserInfo['AccountID'],
			);
		$this->point->insertPoints($data);
        $this->createApi();exit;
    }
	//抽奖信息
	public function choujiangDes(){
		$times = $this->setPoint(10);
        $pointRule = $this->setPoint(11);
		$userInfo = $this->userDetail;
		$mytimes = $this->point->getMyTimes($userInfo['AccountID']);
		$data = [
            'totalTimes'=>$times,
			'pointRule'=>$pointRule,
			'myTimes'=>$mytimes
		];
		$this->createApi($data);exit;
	}
	//抽奖  积分使用
	public function choujiangPoint(){
		//抽奖所需积分
        $userInfo = $this->userDetail;
		$times = $this->setPoint(10);
		$mytimes = $this->point->getMyTimes($userInfo['AccountID']);
		if($times < $mytimes+1)
		{
			$this->errorCode = 90007;
            $this->createApi();exit();
		}
        $choujiang = $this->setPoint(9);
        
        if($choujiang > $userInfo['points']){
            $this->errorCode = 90004;
            $this->createApi();exit();
        }
		
		if(isset($userInfo['AccountID']))
		{
			$result = $this->point->toSendPoints($userInfo['AccountID'],"", $choujiang);
            if(empty($result))
            {
                $this->errorCode = 50006;
                $this->createApi();exit;
            }
            $data = array(
                'AccountID'=>$userInfo['AccountID'],
                'createtime'=>date('Y-m-d',time()),
                'points'=>($choujiang*(-1)),
                'type'=>9,
                );
            $this->point->insertPoints($data);
		}
        $this->createApi();exit;
    }

    public function sendPointHistoryList()
    {
        $this->isArrays = true;
        $userInfo = $this->userDetail;
        $fields = " id, points, AccountID, createtime, toAccountID";
        $list = $this->point->getSendPointHistoryList($userInfo['AccountID'],$fields);
        foreach ($list as $key => $value) {
            $AccountIDInfo = $this->user->get_userinfo_by_id($value['AccountID']);
            $list[$key]['fromUsername'] = $AccountIDInfo['username'];
			$list[$key]['points'] = abs($value['points']);
            $toAccountIDInfo = $this->user->get_userinfo_by_id($value['toAccountID']);
            $list[$key]['toUsername'] = $toAccountIDInfo['username'];
            unset($list[$key]['AccountID'],$list[$key]['toAccountID']);
        }
        $this->createApi($list);exit;
    }
    
}

/* End of file me.php */
/* Location: ./application/controllers/me.php */
